//
//  YSVideoDetailsCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/1/13.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSVideoDetailsCtrl.h"
#import "YSProductNavgationShareBar.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UIImageView+VideoImage.h"
#import "YSHomeDataTool.h"

@interface YSVideoDetailsCtrl ()
@property (nonatomic, strong) YSAdvert *advert;
@property (nonatomic, strong) YSProductNavgationShareBar *shareBar;
@property (weak, nonatomic) IBOutlet UIImageView *videoThumbnail;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoThumbnailTop;
@property (weak, nonatomic) IBOutlet UILabel *videoTitle;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoTitleConstTop;
@property (weak, nonatomic) IBOutlet UILabel *videoDescribe;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *videoDescribeConstTop;

@end

@implementation YSVideoDetailsCtrl

- (instancetype)initWithAdvert:(YSAdvert *)advert{
    self = [super init];
    if (self) {
        _advert = advert;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    
    [self initViews];
    [self requestData];
    
}

- (void)initViews{
    _shareBar = [YSProductNavgationShareBar shareBar];
    [self.shareBar setWidth:SCREENW];
    [self.shareBar.rightBtn setBackgroundImage:[UIImage imageNamed:@"tabar_collect_normal"] forState:UIControlStateNormal];
    [self.shareBar.rightBtn setBackgroundImage:[UIImage imageNamed:@"tabar_collect_press"] forState:UIControlStateHighlighted];
    [self.shareBar.rightBtn setBackgroundImage:[UIImage imageNamed:@"tabar_collect_press"] forState:UIControlStateSelected];
    [self.shareBar.rightBtn addTarget:self action:@selector(tapMark:) forControlEvents:UIControlEventTouchUpInside];
    [self.shareBar setX:0];
    [self.shareBar setY:0];
    [self.view addSubview:self.shareBar];
    
    self.videoThumbnailTop.constant = CGRectGetMaxY(self.shareBar.frame);
    
    UITapGestureRecognizer  *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    [self.videoThumbnail addGestureRecognizer:tap];
}

- (void)requestData{
    BOOL isLogin = NO;
    if (ISLOGIN) {
        isLogin = YES;
    }
    
    [YSHomeDataTool getVideoDetailsWithAdvertId:self.advert.advertId isLogin:isLogin success:^(YSVideoDetailsRes *result) {
        if(result.isSucc){
            [self fillData:result];
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);

    }];
}

- (void)fillData:(YSVideoDetailsRes *)result{
    _advert = result.advert;
    //mark
    if(self.advert.isMark){
        [self.shareBar.rightBtn setSelected:YES];
    }else{
        [self.shareBar.rightBtn setSelected:NO];
    }
    
    YSShareModel *sm = [YSShareModel new];
    [sm setUrl:self.advert.videoShareUrl];
    [sm setTitle:self.advert.advertTitle];
    [sm setSubTile:self.advert.advertText];
    [_shareBar setShareModel:sm];
    
    
    [self.videoThumbnail setImageWithVideoUrl:[NSURL URLWithString:self.advert.advertVideoUrl] placeholderImageStr:@"bg_loding_defalut"];
    
    if (ISBLANK(self.advert.advertTitle)) {
        self.videoTitleConstTop.constant = self.videoTitleConstTop.constant - 15;
    }else{
        [self.videoTitle setText:self.advert.advertTitle];
    }
    
    if (ISBLANK(self.advert.advertText)) {
        self.videoDescribeConstTop.constant = self.videoDescribeConstTop.constant - 15;
    }else{
        [self.videoDescribe setText:self.advert.advertText];
    }
}

// 点击播放视频
- (void)tapClick:(UITapGestureRecognizer *)gestureRecognizer{
    NSURL *movieURL = [NSURL URLWithString:self.advert.advertVideoUrl];
    MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:movieController];
    [movieController.moviePlayer play];
}

// 收藏
- (void)tapMark:(UIButton *)sender{
    if (ISLOGIN) {
        NSInteger isMark = self.shareBar.rightBtn.isSelected == YES ? 2 : 1;
        [YSHomeDataTool markVideoWithVideoId:[NSString stringWithFormat:@"%ld",_advert.advertId] type:isMark success:^(BaseResponse *result) {
            if(result.isSucc){
                if(isMark == 1){
                    [self.shareBar.rightBtn setSelected:YES];
                    ShowTips(@"收藏成功")
                }else{
                    [self.shareBar.rightBtn setSelected:NO];
                    ShowTips(@"已取消收藏")
                }
            }else{
                ShowEnterTips(result.data);
            }
        } failure:^(NSError *error) {
            ShowTips(kRequestFailure)
        }];

    }else{
        [Utils showLoginCtrl:self];
    }
}



@end
